<?php

namespace App\Console\Commands;

use App\Library\Logger;
use App\Models\CityModel;
use App\Models\Customer;
use App\Models\Event;
use App\Models\Product;
use App\Models\WorkOrder;
use Illuminate\Console\Command;

class UpdateZoneCodes extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update:zone_codes';
    private $logger;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新区域code为ID';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->logger = new Logger('update/zone_codes');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $bench = new \Ubench();
        $bench->start();

        $this->updateCustomer();
        $this->updateCustomerUser();
        $this->updateProduct();
        $this->updateWorkOrder();
        $this->updateEvent();

        $bench->end();
        $this->logger->debug("time used: " . $bench->getTime());
        $this->logger->debug("memory used: " . $bench->getMemoryUsage());
    }

    private function updateEvent()
    {
        $this->logger->info('update event');
        $events = Event::all();
        foreach ($events as $event) {
            $this->logger->info("update event, ID: " . $event->id);
            $provinceId = $event->province_id ?? 0;
            $cityId = $event->city_id ?? 0;
            $countyId = $event->county_id ?? 0;

            $zoneData = $this->getZoneData($provinceId, $cityId, $countyId);
            $event->fill($zoneData);
            $event->saveOrFail();
        }
    }

    private function updateWorkOrder()
    {
        $this->logger->info('update wos');
        $workOrders = WorkOrder::all();
        foreach ($workOrders as $workOrder) {
            $this->logger->info("update wo, ID: " . $workOrder->id);
            $provinceId = $workOrder->province_id ?? 0;
            $cityId = $workOrder->city_id ?? 0;
            $countyId = $workOrder->county_id ?? 0;

            $zoneData = $this->getZoneData($provinceId, $cityId, $countyId);
            $workOrder->fill($zoneData);
            $workOrder->saveOrFail();
        }
    }

    private function updateCustomer()
    {
        $this->logger->info('update customers');
        $customers = Customer::all();
        foreach ($customers as $customer) {
            $this->logger->info("update customer, ID: " . $customer->id);
            $provinceId = $customer->province_id ?? 0;
            $cityId = $customer->city_id ?? 0;
            $countyId = $customer->county_id ?? 0;

            $zoneData = $this->getZoneData($provinceId, $cityId, $countyId);
            $customer->fill($zoneData);
            $customer->saveOrFail();
        }
    }

    private function updateProduct()
    {
        $this->logger->info('update products');
        $products = Product::all();
        foreach ($products as $product) {
            $this->logger->info("update product, ID: " . $product->id);
            $provinceId = $product->province_id ?? 0;
            $cityId = $product->city_id ?? 0;
            $countyId = $product->county_id ?? 0;

            $zoneData = $this->getZoneData($provinceId, $cityId, $countyId);
            $product->fill($zoneData);
            $product->saveOrFail();
        }
    }

    private function updateCustomerUser()
    {
        $this->logger->info('update customer users');
        $users = Customer\User::all();
        foreach ($users as $user) {
            $this->logger->info("update customer user, ID: " . $user->id);
            $provinceId = $user->province_id ?? 0;
            $cityId = $user->city_id ?? 0;
            $countyId = $user->county_id ?? 0;

            $zoneData = $this->getZoneData($provinceId, $cityId, $countyId);
            $user->fill($zoneData);
            $user->saveOrFail();
        }
    }

    private function getZoneData($provinceId, $cityId, $countyId)
    {
        $province = CityModel::find($provinceId);
        if (empty($province)) {
            $province = CityModel::byCityCode($provinceId)->first();
        }

        $provinceId = $province->id ?? 0;
        $provinceName = $province->name ?? '';

        $city = CityModel::find($cityId);
        if (empty($city)) {
            $city = CityModel::byCityCode($cityId)->first();
        }

        $cityId = $city->id ?? 0;
        $cityName = $city->name ?? '';

        $county = CityModel::find($countyId);
        if (empty($county)) {
            $county = CityModel::byCityCode($countyId)->first();
        }

        $countyId = $county->id ?? 0;
        $countyName = $county->name ?? '';

        return [
            'province_id' => $provinceId,
            'province_name' => $provinceName,
            'city_id' => $cityId,
            'city_name' => $cityName,
            'county_id' => $countyId,
            'county_name' => $countyName
        ];
    }
}
